<?php
/**
 * Created by PhpStorm.
 * User: 李闯
 * Date: 2020/7/10
 * Time: 9:46
 * Content: 方法
 */
/**
 * @param $level
 * @return mixed
 * @content 用户等级
 */
function getMemberLevel($level){
    if($level == 'level1'){
        $member_level =1;
    }else if($level == 'level2'){
        $member_level =2;

    }else if($level == 'level3'){
        $member_level =3;

    }else if($level == 'level4'){
        $member_level =4;

    }else if($level == 'level5'){
        $member_level =5;

    }else if($level == 'level6'){
        $member_level =6;

    }else if($level == 'level7'){
        $member_level =7;

    }else if($level == 'level8'){
        $member_level =8;

    }else if($level == 'level9'){
        $member_level =9;

    }else if($level == 'level10'){
        $member_level =10;

    }else if($level == 'level11'){
        $member_level =11;

    }else if($level == 'level12'){
        $member_level =12;

    }else{
        $member_level =0;
    }
    return $member_level;

}
/**
 * @param null
 * @return null
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @context 无限极分类 递归
 */
function getTree($array, $pid =0, $level = 0){

    //声明静态数组,避免递归调用时,多次声明导致数组覆盖
    static $list = [];
    foreach ($array as $key => $value){
        //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
        if ($value['pid'] == $pid){
            //父节点为根节点的节点,级别为0，也就是第一级
            $value['level'] = $level;
            //把数组放到list中
            $list[] = $value;
            //把这个节点从数组中移除,减少后续递归消耗
            unset($array[$key]);
            //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
            getTree($array, $value['id'], $level+1);

        }
    }
    return $list;
}
/**
 * @param level  次数
 * @return null
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 * @context level  次数  子集的级别 依次递增
 */
function getPlevel($level){
    $res = str_repeat('|-',$level);
    return $res;

}
/**
 * @access public
 * @param  $price 价格
 * @return str
 * @content 价格小数点留2位
 */
function getPriceInfo($price){
    $info = sprintf("%.2f",$price);
    return $info;
}
/**
 * @param NUll
 * @return string
 * @throws \think\Exception
 * @context 生成订单号
 */
function getOrderNumber()
{
    $rand =substr(mt_rand(12345678945,100000000000000000),4,4) ;
    $str = substr(date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8), 4, 16). $rand;
    return $str;
}
function url(){
    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
    return $http_type . $_SERVER['HTTP_HOST'] . "/uploads/" ;
}
/**
 * @access public
 * @param 参数 $img 内容
 * @return array
 * @content 在字符串中提取图片名字，并且以数组的形式返回
 */
function imgName($imgoldurl){
    preg_match_all('/<img[^>]*?src=".*?\/uploads\/([^"]*?)"[^>]*?>/i', $imgoldurl, $match);
    $img = is_array($match[1]) ? implode(',', $match[1]) : $match[1];
    $newimg = explode(",", $img);
    return $newimg;
}